package others.swing.p019;

import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Insert extends JFrame implements ActionListener {
    JLabel p1, p2, p3, p4, p5, p6, p7;
    JTextField t1, t2, t3, t4, t5;
    JButton btn1, btn2, btn3, btn4, btn5;
    JTextArea ta1, ta2;

    //构造函数，所有控件的设计
    Insert() {
        this.setTitle("校园卡成员插入！");
        p1 = new JLabel("姓名：");
        p2 = new JLabel("学号：");
        p3 = new JLabel("是否进入其他系统？请选择：");
        p4 = new JLabel("密码：");
        p5 = new JLabel("身份：");
        p6 = new JLabel("余额：");
        p7 = new JLabel("信息如下：");


        t1 = new JTextField(10);
        t2 = new JTextField(10);
        t3 = new JTextField(10);
        t4 = new JTextField(10);
        t5 = new JTextField(10);

        ta1 = new JTextArea(100, 300);
        ta2 = new JTextArea(300, 300);

        btn1 = new JButton("确定");
        btn2 = new JButton("取消");
        btn3 = new JButton("查询信息");
        btn4 = new JButton("修改密码");
        btn5 = new JButton("删除成员");

        t1.addActionListener(this);
        t2.addActionListener(this);
        t3.addActionListener(this);
        t4.addActionListener(this);
        t5.addActionListener(this);

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);

        Container c = getContentPane();
        c.setLayout(null);

        c.add(p1);
        c.add(p2);
        c.add(p3);
        c.add(p4);
        c.add(p5);
        c.add(p6);
        c.add(p7);

        c.add(t1);
        c.add(t2);
        c.add(t3);
        c.add(t4);
        c.add(t5);

        c.add(ta1);
        c.add(ta2);

        c.add(btn1);
        c.add(btn2);
        c.add(btn3);
        c.add(btn4);
        c.add(btn5);

        p1.setBounds(50, 10, 50, 40);
        p2.setBounds(50, 50, 50, 40);
        p3.setBounds(100, 520, 250, 40);
        p4.setBounds(50, 90, 50, 40);
        p5.setBounds(320, 10, 50, 40);
        p6.setBounds(320, 50, 50, 40);
        p7.setBounds(100, 170, 100, 40);

        t1.setBounds(100, 10, 100, 30);
        t2.setBounds(100, 50, 100, 30);
        t3.setBounds(100, 90, 100, 30);
        t4.setBounds(360, 10, 100, 30);
        t5.setBounds(360, 50, 100, 30);

        ta1.setBounds(100, 130, 400, 40);
        ta1.setBackground(Color.cyan);
        ta2.setBounds(100, 200, 400, 300);

        btn1.setBounds(280, 90, 80, 30);
        btn2.setBounds(400, 90, 80, 30);
        btn3.setBounds(100, 330, 100, 30);
        btn4.setBounds(280, 520, 100, 30);
        btn5.setBounds(400, 520, 100, 30);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e1) {
                dispose();
                System.exit(0);
            }
        });
        this.setBounds(300, 50, 600, 600);
        this.setVisible(true);
        this.setResizable(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Connection con;
        Statement sql;
        ResultSet rs;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e1) {
            System.out.println(" " + e1);
        }
        if ("确定".equals(e.getActionCommand()))//确定按钮
        {
            try {
                con = DriverManager.getConnection("jdbc:odbc:campus-card", "", "");
                sql = con.createStatement();
                String sno, sname, pwd, type, recode, insertStr;
                Double money;
                sno = t2.getText();
                sname = t1.getText();
                pwd = t3.getText();
                type = t4.getText();
                money = Double.parseDouble(t5.getText());
                recode = "(" + "'" + sno + "'" + "," + "'" + sname + "'" + "," + "'" + pwd + "'" + "," + "'" + type + "'" + "," + "'" + money + "'" + ")";
                insertStr = "INSERT INTO message VALUES" + recode;//插入信息
                sql.executeUpdate(insertStr);
                rs = sql.executeQuery("SELECT * FROM message");
                while (rs.next())//遍历输出剩余信息
                {
                    String sno1 = rs.getString(1);
                    String sname1 = rs.getString(2);
                    String type1 = rs.getString(4);
                    Double money1 = Double.parseDouble(rs.getString(5));
                    ta1.setText("插入成功！");
                    ta2.append("学号:  " + sno1 + "  姓名:  " + sname1 + "  身份:  " + type1 + "  余额:  " + money1 + '\n');
                }
                con.close();
            } catch (SQLException e1) {
                System.out.println(e1);
            }
        }
        /*其他按钮的操作*/
        else if ("查询信息".equals(e.getActionCommand())) {
            new Search().setVisible(true);
            this.dispose();
        } else if ("修改密码".equals(e.getActionCommand())) {
            new Change().setVisible(true);
            this.dispose();
        } else if ("删除成员".equals(e.getActionCommand())) {
            new Delete().setVisible(true);
            this.dispose();
        }
    }

    public static void main(String args[]) {
        new Insert();
    }
}
